package com.ewhale.feitengguest.ui.user.message.chat;

import android.app.Activity;

import com.hyphenate.EMError;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.simga.library.utils.LogUtil;

/**
 * Author : zhouyx
 * Date   : 2017/8/31
 * Description :
 */
public class AuthHelper {

    private static final String TAG = AuthHelper.class.getSimpleName();

    private static final AuthHelper ourInstance = new AuthHelper();

    public static AuthHelper getInstance() {
        return ourInstance;
    }

    private AuthHelper() {
    }

    /**
     * 创建环信账号
     *
     * @param username
     */
    private void createAccount(final Activity activity, final String username, final AuthCallback callback) {
        ChatClient.getInstance().register(username, Constant.DEFAULT_ACCOUNT_PWD, new Callback() {
            @Override
            public void onSuccess() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        login(activity, username, Constant.DEFAULT_ACCOUNT_PWD,callback);
                    }
                });
            }
            @Override
            public void onError(int i, String s) {
                LogUtil.e("JIA", "---登录注册错误码-- : " + i + "--注册环信错误信息--" + s);
                if (i == EMError.USER_ALREADY_EXIST) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            login(activity, username,Constant.DEFAULT_ACCOUNT_PWD, callback);
                        }
                    });
                } else {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (callback != null) {
                                callback.onError(i,s);
                            }
                        }
                    });
                }
            }
            @Override
            public void onProgress(int i, String s) {
            }
        });
    }

    /**
     * 登录环信客服
     */
    public void login(final Activity activity, final String username,final String password, final AuthCallback callback) {
        if (ChatClient.getInstance().isLoggedInBefore()) {
            if (callback != null) {
                callback.onSuccess();
            }
            return;
        }
        ChatClient.getInstance().login(username, password, new Callback() {
            @Override
            public void onSuccess() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                });
            }
            @Override
            public void onError(int i, String s) {
                LogUtil.e("JIA","环信登录失败错误码 ： " + i+" , 环信登录失败原因："+s);
//                if (i == EMError.USER_NOT_FOUND) {
//                    createAccount(activity, username, callback);
//                    return;
//                }
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onError(i,s);
                        }
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {
            }
        });
    }

    /**
     * 退出环信登录
     */
    public void logout(final Activity activity, final AuthCallback callback) {
        ChatClient.getInstance().logout(true, new Callback() {
            @Override
            public void onSuccess() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            callback.onError(i,s);
                        }
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {
            }
        });
    }

    public interface AuthCallback {
        void onSuccess();

        void onError(int errorCode,String errorStr);
    }

}
